package com.bmps.springcloud.insuredmicroservice.service.trial.strategy;

import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.InsurantModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.trail.TrialPersonModel;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.trial.TrialUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author : 追到乌云的尽头找太阳-（Jacob）
 * @date : 2020/2/12 14:06
 **/
public class AAT046TrialStrategy implements TrialStrategy{
    
    private static final Logger logger = LoggerFactory.getLogger(AAT046TrialStrategy.class);
    
    @Override
    public TrialPersonModel premiumTrialCalculationPrepare(ProductModel productModel, RequestModel requestModel, RedisService redisService) {
        List<InsurantModel> insurantModelList = requestModel.getCustInfo().getInsurants().getInsurant();
        for(InsurantModel insurantModel :insurantModelList) {
            if (insurantModel.getInsSeq() == productModel.getProdInsSeq()) {
                TrialPersonModel trialPersonModel = new TrialPersonModel();
                String riskCode = productModel.getProdCode();
                trialPersonModel.setRiskCode(riskCode);
                StringBuilder sb = new StringBuilder(16);
                sb.append(AcceptConstant.REDIS_PREFIX_R004).append("|").append(riskCode);
                Object object = redisService.get(sb.toString());
                if (object == null) {
                    return new TrialPersonModel();
                }
                TrialUtil.setSpecialElement(trialPersonModel, object.toString());
                logger.info("【" + productModel.getProdCode() + "】【VPU】【KEY】" + sb.toString());
                
                sb.setLength(0);
                sb.append(AcceptConstant.REDIS_PREFIX_R006).append("|").append(trialPersonModel.getRiskCode()).append("|G");
                object = redisService.get(sb.toString());
                if (object == null) {
                    return new TrialPersonModel();
                }
                trialPersonModel.setFormula(object.toString());
                logger.info("【" + productModel.getProdCode() + "】【公式】【KEY】" + sb.toString());
                
                Integer planVersion = productModel.getPlanVersion();
                if (1 == planVersion){
                    trialPersonModel.setPlanVersion(68);
                }else if(2 == planVersion){
                    trialPersonModel.setPlanVersion(100);
                }
                else if (3 == planVersion){
                    trialPersonModel.setPlanVersion(168);
                }
                trialPersonModel.setInsuYearFlag(productModel.getInsureYearFlag());
                if (StringUtils.equals("M", productModel.getInsureYearFlag())) {
                    
                    Integer insureYear = productModel.getInsureYear();
                    if (1 == insureYear){
                        trialPersonModel.setPayIntvRate("0.2");
                    }else if(2 == insureYear){
                        trialPersonModel.setPayIntvRate("0.3");
                    }else if(3 == insureYear){
                        trialPersonModel.setPayIntvRate("0.4");
                    }else if(4 == insureYear){
                        trialPersonModel.setPayIntvRate("0.5");
                    }else if(5 == insureYear){
                        trialPersonModel.setPayIntvRate("0.6");
                    }else if(6 == insureYear){
                        trialPersonModel.setPayIntvRate("0.7");
                    }else if(7 == insureYear){
                        trialPersonModel.setPayIntvRate("0.75");
                    }else if(8 == insureYear){
                        trialPersonModel.setPayIntvRate("0.8");
                    }else if(9 == insureYear){
                        trialPersonModel.setPayIntvRate("0.85");
                    }else if (10 == insureYear){
                        trialPersonModel.setPayIntvRate("0.9");
                    }else if(11 == insureYear){
                        trialPersonModel.setPayIntvRate("0.95");
                    }
                }else {
                    trialPersonModel.setPayIntvRate("1");
                }
                sb.setLength(0);
                sb.append(AcceptConstant.REDIS_PREFIX_R005).append("|").append(riskCode).append("|G");
                object = redisService.get(sb.toString());
                if (object == null) {
                    return new TrialPersonModel();
                }
                logger.info("【"+riskCode+"】【产品计算公式要素配置】【KEY】"+sb.toString());
                TrialUtil.setElementMapping(trialPersonModel, object.toString());
                return trialPersonModel;
            }
        }
        return new TrialPersonModel();
    }
}
